package com.quyeying.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.service.TblMerchantService;
import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.kdn.KdnUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblOrderBaseService;
import com.quyeying.entity.TblOrderBaseEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
* <p>
* tbl_order_base 前端控制器
* </p>
* @author skx
* @since 2022-07-19
*/
@Slf4j
@RestController
@RequestMapping("orderBaseEntity")
public class TblOrderBaseController {

    @Resource
    private TblOrderBaseService tblOrderBaseService;
    @Resource
    KdnUtil kdnUtil;
    @Resource
    private TblMerchantService tblMerchantService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody TblOrderBaseEntity tblOrderBaseEntity){
        try {
            return tblOrderBaseService.add(tblOrderBaseEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblOrderBaseService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblOrderBaseEntity tblOrderBaseEntity){
        try {
            return tblOrderBaseService.updateData(tblOrderBaseEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage( Integer current, Integer size, Integer orderStatus
            ,String search, String searchType, String orderType, HttpServletRequest request
            , String sort, String startTime, String endTime){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            return tblOrderBaseService.findListByPage(current, size, orderStatus,search, searchType, orderType, merchantInfo.getId(), sort, startTime, endTime);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblOrderBaseService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 发货
     */
    @PostMapping("/ship")
    public R ship(@RequestBody TblOrderBaseEntity orderBaseEntity) {
        TblOrderBaseEntity baseEntity = tblOrderBaseService.getById(orderBaseEntity.getId());
        if(baseEntity.getOrderStatus() == 1) {
            tblOrderBaseService.ship(orderBaseEntity);
            return R.success();
        }
        return R.error(ResultEnum.SYSTEM_ERROR.getCode(), "订单状态错误，发货失败");
    }

    /**
     * 查询快递信息
     */
    @GetMapping("/queryExpressInfo")
    public R queryExpressInfo(String shipperCode, String logisticCode){
        try {
            JsonNode resJson = kdnUtil.expressQuery(shipperCode, logisticCode);
            return R.success(resJson);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 导出订单
     *  orderStatus, String startTime, String endTime
     */
    @PostMapping ("/export")
    public void export(HttpServletResponse response, HttpServletRequest request, @RequestBody Map<String, Object> reqParam) {
        String authorization = request.getHeader("Authorization");
        String userName = JwtUtil.getUserName(authorization);
        TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
        tblOrderBaseService.exportWaitShipOrderInfo(response,merchantInfo.getId(), reqParam);
    }

    /**
     * 审核退款
     */
    @PostMapping("/refund")
    public R wxPay(HttpServletRequest request, @RequestBody JsonNode jsonNode) {
        return tblOrderBaseService.refund(request, jsonNode);
    }
}
